package com.caimore.modulefactory;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;

public class SmsFactory {
    private ReentrantLock m_WriterLock = new ReentrantLock();
    private ReentrantLock m_ReaderLock = new ReentrantLock();
    private ConcurrentHashMap<String, IsmsWriter> m_lstWriterInstance = new ConcurrentHashMap<>();
    private ConcurrentHashMap<String, IsmsReader> m_lstReaderInstance = new ConcurrentHashMap<>();
    IsmsWriter iWriter = null;
    IsmsReader iReader = null;
    private static SmsFactory m_instance = new SmsFactory();

    private SmsFactory() {
    }

    public static SmsFactory getInstance() {
        return m_instance;
    }

    public void pushWriterInstance(String sSmsType, IsmsWriter oIsmsWriter) {
        try {
            this.m_WriterLock.lock();
            this.m_lstWriterInstance.put(sSmsType, oIsmsWriter);
        } finally {
            this.m_WriterLock.unlock();
        }
    }

    public IsmsWriter popWriterInstance(String sSmsType) {
        IsmsWriter oIsmsWriter = null;
        try {
            this.m_WriterLock.lock();
            if (this.m_lstWriterInstance.containsKey(sSmsType)) {
                oIsmsWriter = this.m_lstWriterInstance.get(sSmsType);
            }
        } finally {
            this.m_WriterLock.unlock();
        }
        return oIsmsWriter;
    }

    public void pushReaderInstance(String sSmsType, IsmsReader oIsmsReader) {
        try {
            this.m_ReaderLock.lock();
            this.m_lstReaderInstance.put(sSmsType, oIsmsReader);
        } finally {
            this.m_ReaderLock.unlock();
        }
    }

    public IsmsReader popReaderInstance(String sSmsType) {
        IsmsReader oIsmsReader = null;
        try {
            this.m_ReaderLock.lock();
            if (this.m_lstReaderInstance.containsKey(sSmsType)) {
                oIsmsReader = this.m_lstReaderInstance.get(sSmsType);
            }
        } finally {
            this.m_ReaderLock.unlock();
        }
        return oIsmsReader;
    }
}

